﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Transactions;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Sellers;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class SellersTypeController : BaseController
    {
        //
        // GET: /SellersType/
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(SellersTypeModel model)
        {
            var total = 0;
            var list = ServiceFactory.SellersTypeServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                o.SellerTypeName,
                o.IsAvailable,
                CreateTime = o.CreateTime.ToString("yyyy-MM-dd hh:mm:ss")
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加和更新销售商类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateSellersType(SellersType model)
        {
            var serv = ServiceFactory.SellersTypeServiceInstance;
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            if (model.ID > 0)
            {
                var entity = serv.GetSellersTypeById(model.ID);
                if (entity != null)
                {
                    entity.SellerTypeName = model.SellerTypeName;
                    entity.IsAvailable = model.IsAvailable;
                    entity.LastModifyTime = DateTime.Now;
                    entity.LastModifyBy = CurrentUser.UserAccount;
                    responseResult.Success = serv.UpdateEntities(entity);
                }
                else
                {
                    responseResult.Message = "销售商类型不存在";
                }
            }
            else
            {
                if (serv.IsExist(model.SellerTypeName.Trim()))
                {
                    responseResult.Message = "销售商类型名称已存在";
                }
                else
                {
                    model.LastModifyTime = DateTime.Now;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = CurrentUser.UserAccount;
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                }
            }
            return Json(new
            {
                success = responseResult.Success,
                message = responseResult.Message
            });
        }
        #region 修改经销商类型IsAvailable状态
        [HttpPost]
        public JsonResult UpdateSellersTypeStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.SellersTypeServiceInstance.GetSellersTypeById(id);
            var success = false;
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyBy = CurrentUser.UserAccount;
                success = ServiceFactory.SellersTypeServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "成功" : "失败"
            });
        }
        #endregion
	}
}